﻿// mouse_keyboard.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"


#include "mouse_keyboard.h"
#include <windows.h>

HHOOK g_hKeyboardHook = NULL;
HHOOK g_hMouseHook = NULL;

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0 && wParam == WM_KEYDOWN) {
        KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;
        // 在这里处理按键事件
    }
    return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam);
}

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0) {
        MSLLHOOKSTRUCT* p = (MSLLHOOKSTRUCT*)lParam;
        // 在这里处理鼠标事件
    }
    return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam);
}

extern "C" MOUSE_KEYBOARD_API bool InstallKeyboardHook() {
    if (!g_hKeyboardHook) {
        g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
    }
    return g_hKeyboardHook != NULL;
}

extern "C" MOUSE_KEYBOARD_API bool UninstallKeyboardHook() {
    if (g_hKeyboardHook) {
        UnhookWindowsHookEx(g_hKeyboardHook);
        g_hKeyboardHook = NULL;
    }
    return true;
}

extern "C" MOUSE_KEYBOARD_API bool InstallMouseHook() {
    if (!g_hMouseHook) {
        g_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, GetModuleHandle(NULL), 0);
    }
    return g_hMouseHook != NULL;
}

extern "C" MOUSE_KEYBOARD_API bool UninstallMouseHook() {
    if (g_hMouseHook) {
        UnhookWindowsHookEx(g_hMouseHook);
        g_hMouseHook = NULL;
    }
    return true;
}